package com.maxafaa.activitylifecircle;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainSurfaceView extends SurfaceView  implements SurfaceHolder.Callback, Runnable{

	private boolean m_is_running;
	private SurfaceHolder m_surfaceholder;
	private Paint m_paint;
	private Thread m_mainthread;
	private Canvas m_lockcanvas;
	private String m_str_status_text = "";
	Rect m_bounds;
	
	public MainSurfaceView(Context context) {
		super(context);
		m_surfaceholder = getHolder();
		m_surfaceholder.addCallback(this);		
		setFocusable(true);		
	}

	private void Initialize(){
		m_paint = new Paint();
		m_mainthread = new Thread(this);
		m_mainthread.start();
		m_is_running = true;
		
		
	}
	
	public void GameCreate(){
		m_str_status_text = m_str_status_text + "  " + "onCreate()";
		System.out.println("onCreate()");
	}
	
	public void GameStart(){
		m_str_status_text = m_str_status_text + "  " + "onStart()";
		System.out.println("onStart()");
	}
	
	public void GamePause(){
		m_str_status_text = m_str_status_text + "  " + "onPause()";
		System.out.println("onPause()");
	}
	
	public void GameResume(){
		m_str_status_text = m_str_status_text + "  " + "onResume()";
		System.out.println("onResume()");
	}
	
	public void GameStop(){
		m_str_status_text = m_str_status_text + "  " + "onStop()";
		System.out.println("onStop()");
	}
	
	public void GameDestroy(){
		m_str_status_text = m_str_status_text + "  " + "onDestroy()";
		System.out.println("onDestroy()");
	}
	
	public void GameRestart(){
		m_str_status_text = m_str_status_text + "  " + "onRestart()";
		System.out.println("onRestart()");
	}
	
	private void GameUpdate(){
		
	}
	
	private void GameDraw(Canvas canvas){
		canvas.drawColor(Color.WHITE);
		m_paint.setColor(Color.BLACK);
		canvas.drawText(m_str_status_text, 5, 15, m_paint);
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent e){
		super.onTouchEvent(e);
		return true;
	}


	@Override
	public void run() {
		while(m_is_running){
			try{
				// Thread.sleep(20);
			}catch(Exception e){}
			GameUpdate();
			m_lockcanvas = m_surfaceholder.lockCanvas(null);
			synchronized (m_surfaceholder) {
				if(m_lockcanvas != null){
					GameDraw(m_lockcanvas);
					m_surfaceholder.unlockCanvasAndPost(m_lockcanvas);
				}
			}
		}		
	}

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		Initialize();		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		boolean retry = true;
		m_is_running = false;
        do {
            try {
                m_mainthread.join();
                retry = false;
            } catch (InterruptedException e) {
				e.printStackTrace();
                // we will try it again and again...
            }
        }while(retry);		
	}
}
